package edu.sias.chat.controller;

import edu.sias.chat.endpoint.ChatEndpoint;
import edu.sias.chat.service.Impl.UserService;
import edu.sias.chat.vo.UserVO;
import edu.sias.common.core.domain.AjaxResult;
import edu.sias.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    private final UserService userService;  // 假设有用户信息服务

    @Autowired
    public ChatController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/users")
    public AjaxResult getOnlineUsers() {
        // 获取所有在线用户（从 WebSocket 会话管理）
        List<String> onlineUsernames = ChatEndpoint.getOnlineUsernames();

        String username = SecurityUtils.getUsername();

        onlineUsernames.remove(username);

        // 获取用户详细信息（从数据库或其他服务）
        List<UserVO> users = userService.selectAllByUsernames(onlineUsernames);

        return AjaxResult.success(users);
    }
}
